from abc import abstractmethod, ABC
import os
import re


class LogPathStrategy(ABC):
    @abstractmethod
    def get_logs_path(self, rule):
        pass


class RegextRuleStrategy(LogPathStrategy):
    def get_logs_path(self, rule):
        """
        获取日志路径
        :param rule: 日志路径规则
        :return: 日志路径
        """
        file_paths = []
        # 目录
        directory = rule['regex_rule'][0]
        for filename in os.listdir(directory):
            # 正则匹配对应的文件名称
            if re.match(rule['regex_rule'][1], filename):
                file_paths.append(os.path.join(directory, filename))
        return file_paths


class LogPathStrategyFactory:
    _strategy_mapping = {
        'regex_rule': RegextRuleStrategy
    }

    @classmethod
    def get_strategy(cls, rule_type):
        try:
            strategy_class = cls._strategy_mapping[rule_type]
            return strategy_class()
        except KeyError:
            raise NotImplementedError(f'暂不支持该规则类型: {rule_type}')
